<template>
  <BaseMainSlot ref="BaseMainSlot">
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">故障类型：</div>
          <el-select
            clearable
            v-model="hitType"
            placeholder="请选择"
            @change="selectHitchTypeCode(hitType)"
          >
            <el-option
              v-for="item in hitchTypeArr"
              :key="item.hitchTypeCode"
              :label="item.hitchTypeName"
              :value="item.hitchTypeCode"
            ></el-option>
          </el-select>
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">上报日期：</div>
          <el-date-picker
            v-model="applyDate"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            clearable
            @change="queryPageList()"
          />
        </div>
        <div class="operationBtn">
          <div class="screenBtn">
            <div class="btn" @click="changeSelect()">
              <i
                :class="activeName === '' ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"
                class="arrow-down"
              ></i>
            </div>
          </div>
        </div>
        <el-collapse v-model="activeName" accordion @change="handleChange">
          <el-collapse-item name="1">
            <div class="select-box-content">
              <div class="select-box-content-name">设备名称：</div>
              <div
                class="select-box-content-all"
                @click="cancelSelect('equipmentCode')"
              >全部</div>
              <div
                v-for="(item, index) in equipmentArr"
                :key="index"
                class="select-box-content-list"
                :style="{
                  color:
                    equipmentCode === item.equipmentCode
                      ? '#54DEE2'
                      : '#C1CFD2',
                  fontWeight:
                    equipmentCode === item.equipmentCode ? '600' : '400'
                }"
                @click="selectEquipmentCode(item.equipmentCode)"
              >{{ item.equipmentName }}</div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </template>
    <template v-slot:typenodeslot>
      <CheckButton
        :topitem-select-list="topitemSelectList"
        @topitemtype="topitemtype"
      />
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        v-if="topitemtypeId === '待审核'"
        :add-active="addActive"
        :del-active="delActive"
        :page-no="141"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="hitchApplyAuditCode"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        :table-column-sum="topitemtypeId"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template slot="applyDate" slot-scope="scoped">
          <span
            v-if="scoped.row.hitchApplyAuditCode"
          >{{ dateToFormatTwo(scoped.row.applyDate) }}</span>
        </template>
        <template slot="pictures" slot-scope="scoped">
          <span
            v-if="scoped.row.hitchInfoCode"
            class="table-btns-box"
            @click="queryPicture(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看图片</span>
          </span>
        </template>
        <template slot="videos" slot-scope="scoped">
          <span
            class="table-btns-box"
            v-if="scoped.row.hitchInfoCode"
            @click="queryVideo(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看视频</span>
          </span>
        </template>

        <template slot="hitchApplyAuditPersonMapper" slot-scope="scoped">
          <span
            class="table-btns-box"
            v-if="scoped.row.hitchApplyAuditCode&&scoped.row.approvalType&&scoped.row.reason !== '委内维修不需要审核'"
            @click="queryhitchApplyAudit(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看审批进度</span>
          </span>
        </template>
        <template
          slot="operate"
          slot-scope="scoped"
          v-if="scoped.row.hitchInfoCode"
        >
          <span
            class="table-btns-box"
            v-if="(topitemtypeId === '审核通过'||topitemtypeId === '审核未通过')"
            @click="seeInfo(scoped.row)"
          >
            <i class="el-icon-circle-check"></i>
            <span>查看详情</span>
          </span>
          <span
            class="table-btns-box"
            @click="queryReason(scoped.row)"
            v-if="topitemtypeId === '审核未通过'"
          >
            <i class="el-icon-view"></i>
            <span>查看原因</span>
          </span>
          <span
            class="table-btns-box"
            @click="edit(scoped.row)"
            v-if="topitemtypeId === '待审核' && judgePermission(141, 'edit')"
          >
            <i class="el-icon-edit-outline"></i>
            <span>修改</span>
          </span>

          <span
            class="table-btns-box table_del_btn"
            @click="delOne(scoped.row)"
            v-if="topitemtypeId === '待审核' && judgePermission(141, 'deleteone')"
          >
            <i class="el-icon-delete"></i>
            <span>删除</span>
          </span>
        </template>
      </e-table>
    </template>
    <template slot="dialog">
      <HandleInfoDialog
        ref="HandleInfoDialog"
        :edit-object="editObject"
        :show.sync="showReasonDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <AddEditDialog
        ref="addEditDialog"
        :hitch-type-arr="hitchTypeArr"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        :userList="userList"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
        @updataHit="queryPageList()"
      />
      <HitchApply
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showhitchApplyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Video
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showVideoDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Picture
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showPictureDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Key
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showKeyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <see-info-dialog
        :showInfo="seeInfoDialog"
        :seeInfoObject="editObject"
        :titleText="titleText"
        @closeDialog="closeAll"
      ></see-info-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import failureToReportApi from "@/api/equip-repair-manage/failure-to-report";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import HandleInfoDialog from "../components/handle-info-dialog.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import CheckButton from "@/components/equipTypeSelectButton/selcetButton.vue";
import repairTypeApi from "@/api/equip-repair-manage/repair-type";
import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record";
import HitchApply from "@/components/detailDialog/hitchApply-dialog.vue";
import userApi from "@/api/system-settings/user-manage";
import Video from "@/components/detailDialog/video-dialog.vue";
import Picture from "@/components/detailDialog/picture-dialog.vue";
import Key from "@/components/detailDialog/key-dialog.vue";
import { mapGetters } from "vuex";
import seeInfoDialog from "./components/show-dialog.vue";
export default {
  name: "FailureToReport",
  components: {
    BaseMainSlot,
    HandleInfoDialog,
    CheckButton,
    ActionBar,
    AddEditDialog,
    Video,
    HitchApply,
    Picture,
    Key,
    seeInfoDialog,
  },
  mixins: [mymixin],
  data() {
    return {
      hitType: "",
      // 搜索条件:故障名称：
      hitchTypeCode: "",
      hitchTypeArr: [],
      //  搜索条件:设备名称：
      equipmentCode: "",
      equipmentArr: [],
      //  搜索条件:上报日期
      applyDate: "",
      // 待审核,审核通过,审核未通过
      topitemtypeId: "待审核",
      topitemSelectList: [
        { id: "待审核", name: "待审核" },
        { id: "审核通过", name: "审核通过" },
        { id: "审核未通过", name: "审核未通过" },
      ],
      userList: [],
      // 表单展示项
      tableLabel: [
        {
          label: "设备名称",
          param: "equipmentName",
          selectValue: "equipmentCode", // Select 实际查询参数
          show: true,
          type: "Select",
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["equipmentName", "equipmentCode"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "故障类型名称",
          param: "hitchTypeName", // 表格显示参数
          selectValue: "hitchTypeCode", // Select 实际查询参数
          show: true,
          type: "Select", // input Select Checkbox date
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["hitchTypeName", "hitchTypeCode"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "上报人",
          param: "proposer",
          selectValue: "userId", // Select 实际查询参数
          show: true,
          type: "Select", // input Select Checkbox date
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["username", "userId"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "上报日期",
          param: "applyDate",
          slotName: "applyDate",
          type: "datetime",
          show: true,
        },
        {
          label: "维修方式",
          param: "repairNature",
          show: true,
        },
        {
          label: "故障图片",
          param: "pictures",
          show: true,
          slotName: "pictures",
          notSearch: true,
        },
        {
          label: "故障视频",
          param: "videos",
          show: true,
          slotName: "videos",
          notSearch: true,
        },
        {
          label: "审批详情",
          param: "hitchApplyAuditPersonMapperList",
          slotName: "hitchApplyAuditPersonMapper",
          show: true,
          notSearch: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            type: "text",
            icon: "el-icon-circle-check",
            methods: "operate",
            slotName: "operate",
          },
        ],
      },
      seeInfoDialog: false,
      showhitchApplyDialog: false,
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {
    showAddEditDialog(v) {
      if (!v) {
        this.closeDialog();
      }
    },
  },
  created() {
    this.queryEquipment();
    this.queryhitchTypeList();
    this.queryUserList();
  },
  mounted() {},
  methods: {
    queryhitchApplyAudit(row) {
      this.titleText = "审核详情";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showhitchApplyDialog = true;
    },
    closeAll() {
      this.seeInfoDialog = false;
    },
    // 清空选项
    cancelSelect(data) {
      if (data === "hitchTypeCode") {
        this.hitchTypeCode = "";
      }
      if (data === "equipmentCode") {
        this.equipmentCode = "";
      }
      this.queryPageList();
    },
    // 选择
    selectHitchTypeCode(id) {
      if (this.hitchTypeCode === id) {
        this.hitchTypeCode = "";
      } else {
        this.hitchTypeCode = id;
      }
      this.queryPageList();
    },
    // 选择
    selectEquipmentCode(id) {
      if (this.equipmentCode === id) {
        this.equipmentCode = "";
      } else {
        this.equipmentCode = id;
      }
      this.queryPageList();
    },
    // 条件查询未报废设备
    queryEquipment() {
      this.equipmentArr = [];
      let params = {};
      equipScrapRecordApi.queryEquipment(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.equipmentArr.push({
              equipmentName: data[i],
              equipmentCode: i,
            });
          }
          this.setTableColumnsPopover("equipmentName", this.equipmentArr);
        }
      });
    },
    // 条件查询故障类型
    queryhitchTypeList() {
      this.hitchTypeArr = [];
      repairTypeApi.findAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.hitchTypeArr = data;
          this.setTableColumnsPopover("hitchTypeName", this.hitchTypeArr);
        }
      });
    },
    // 查询可选择维修人
    queryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.userList = data;
          this.setTableColumnsPopover("proposer", this.userList);
        }
      });
    },
    //表头 下拉框输入赋值
    setTableColumnsPopover(key, data) {
      let item = null;
      let index = 0;
      this.tableLabel.forEach((el, i) => {
        if (el.param === key && el.typeObj) {
          item = el;
          index = i;
          item.typeObj.list = data;
        }
      });
      item && this.$set(this.tableLabel, index, item);
    },
    // 转换故障类型编号到名字
    changeHitchTypeNameByHitchTypeCode(v) {
      var index = this.hitchTypeArr.findIndex(
        (item) => item.hitchTypeCode === v
      );
      if (index !== -1) {
        return this.hitchTypeArr[index]["hitchTypeName"];
      }
    },
    //  待审核,审核通过,审核未通过
    topitemtype(e) {
      this.topitemtypeId = e;
      // setTimeout(() => {
      //   this.$refs.BaseMainSlot.calculateHeight();
      // }, 200);
      // 获取表格数据
      this.mixTableUpdatePlus();
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          hitchTypeCode: this.hitchTypeCode,
          equipmentCode: this.equipmentCode,
          applyDate: this.applyDate,
          result: this.topitemtypeId,
          ...e,
          // repairNature: "委内维修"
        },
      };
      // if (this.topitemtypeId === '待审核') {
      //   param.entity.latest = true
      // }

      failureToReportApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        if (item.hitchApplyAuditCode) {
          return item.hitchApplyAuditCode;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          var form = new FormData();
          form.append("hitchApplyAuditCodeList", data.hitchApplyAuditCode);
          failureToReportApi.delList(form).then((res) => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach((a) => {
        form.append("hitchApplyAuditCodeList", a);
      });
      failureToReportApi.delList(form).then((res) => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 查看原因
    queryReason(row) {
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showReasonDialog = true;
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.editActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.hitchApplyAuditCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
    // 查看详情
    seeInfo(row) {
      this.titleText = "故障详情";
      this.editObject = JSON.parse(JSON.stringify(row));
      console.log(this.editObject);
      this.seeInfoDialog = true;
    },
    // 查看关键字
    queryKey(row) {
      this.titleText = "关键字";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showKeyDialog = true;
    },
    // 查看视频
    queryVideo(row) {
      this.titleText = "故障视频";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showVideoDialog = true;
    },
    // 查看图片
    queryPicture(row) {
      this.titleText = "故障图片";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showPictureDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table {
  margin-top: 20px;
}
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .content-manage .common-layout {
  margin-top: 30px;
}
::v-deep .butt-list-sty {
  top: -15px;
}

::v-deep .common-layout {
  position: relative;
  margin-top: 20px;
  .action-bar {
    position: absolute;
    width: 300px;
    top: 5px;
    right: 20px;
  }
}
</style>
